/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2011
Version 0.1
=======================================================*/
ACSMainPanel = function(){
    ACSMainPanel.superclass.constructor.call(this, {activeTab:0, region:'center', margins:'5 0 25 0', resizeTabs:true, tabWidth:150, minTabWidth: 120, enableTabScroll: true, plugins: new Ext.ux.TabCloseMenu()});
        											
	this.addEvents({refresh:true,refreshtree:true,refreshdevice:true});
	
	this.on('refresh', this.onRefresh, this);
	this.on('refreshdevice', this.onRefreshDevice, this);
};

Ext.extend(ACSMainPanel, Ext.TabPanel, {
	onRefresh : function(){
		for(var i=0; i<this.items.length; i++) {
			this.items.get(i).refresh();
		}
	},	

	onRefreshDevice : function(device,old){
		if(this.getItem(device.tid)) {
			this.getItem(device.tid).refresh();
		}
		
		if(this.getItem('c'+device.configID)) {
			this.getItem('c'+device.configID).refresh(this.getItem('c'+device.configID).configuration);
		}

		if(this.getItem('g'+device.groupID)) {
			this.getItem('g'+device.groupID).refresh(this.getItem('g'+device.groupID).group);
		}
		
		if(old) {
			if(old.configID != device.configID && this.getItem('c'+old.configID)) {
				this.getItem('c'+old.configID).refresh();
			}
	
			if(old.groupID != device.groupID && this.getItem('g'+old.groupID)) {
				this.getItem('g'+old.groupID).refresh();
			}
		}
	},	

    load : function(obj){
    	if(this.getItem(obj.tid)) {
			this.activate(this.getItem(obj.tid));
    	} else if(obj instanceof Group) {
    		var panel = new GroupPanel(obj,this);
			this.add(panel);
			this.activate(panel);
    	} else if(obj instanceof Configuration) {
    		var panel = new ConfigurationPanel(obj,this);
			this.add(panel);
			this.activate(panel);
    	} else if(obj instanceof Device) {
    		var panel = new DevicePanel(obj,this);
			this.add(panel);
			this.activate(panel);
    	}
    },    
    	
	reload : function(obj){
		if(this.getItem(obj.tid)) {
			this.getItem(obj.tid).refresh(obj);
		}
	},    
    
    unload : function(obj){
    	if(obj && this.getItem(obj.tid)) {
    		this.remove(this.getItem(obj.tid));
    	}
    }
});

Ext.reg('appmainpanel', ACSMainPanel);